package cm.mefobe.android.telecomcost.activities;

import java.util.ArrayList;
import java.util.Calendar;
import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.adapter.VoiceRateAdapter;
import cm.mefobe.android.telecomcost.entities.Call;
import cm.mefobe.android.telecomcost.entities.Destination;
import cm.mefobe.android.telecomcost.entities.VoiceRate;
import cm.mefobe.android.telecomcost.entities.Zone;
import cm.mefobe.android.telecomcost.view.LabeledEditText;
import cm.mefobe.android.telecomcost.view.TimeIntervallView;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ListView;

public class CostEstimatorActivity extends TelecomcostActivity {

	private ListView list;

	private ArrayList<Zone> _zones;
	private LabeledEditText numberView;
	private TextView lblFees;
	private TimeIntervallView lenghView;
	private Button btnStart;

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("NumberInfoActivity", "Creating the activity");

		super.onCreate(savedInstanceState);

		setContentView(R.layout.cost_estimations);

		btnStart = (Button) findViewById(R.id.btnStart);
		numberView = (LabeledEditText) findViewById(R.id.pickerView1);
		lenghView = (TimeIntervallView) findViewById(R.id.lenghView);
		lblFees = (TextView) findViewById(R.id.lblFees);		
		list = (ListView) findViewById(R.id.listViewVoiceRates);

		lenghView.setLabel ("Call lengh ");
		numberView.setLabel("Number     ");
		numberView.getEditText().setInputType(InputType.TYPE_CLASS_PHONE);
		numberView.getEditText().setWidth(lenghView.getEditText().getWidth() + lenghView.getPicker().getWidth());
		
		btnStart.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showNumberInfos(numberView.getText());
			}
		});
	}

	protected void showNumberInfos(String number) {
		Log.i("NumberInfoActivity", "showing infos for number..." + number);
		Call fakeCall = new Call(number,Calendar.getInstance().getTime());
		Destination dest = fakeCall.getDestination(_zones);		
		
		if (dest != null) {
			list.setAdapter(new VoiceRateAdapter(
					CostEstimatorActivity.this, R.layout.item_row,
					dest.getVoiceRates()));
			showCosts(fakeCall);
		} else {	
			lblFees.setText("unknown destination, please configure the zone, country and provider for this destination");
			list.setAdapter(new VoiceRateAdapter(
					CostEstimatorActivity.this, R.layout.item_row,
					new ArrayList<VoiceRate>()));
		}
	}

	private void showCosts(Call fakeCall) {
		VoiceRate rate = fakeCall.getCallRate();
		Log.i("NumberInfoActivity", "displaying estimation for rate...");
		if (rate != null) {
			lblFees.setVisibility(View.VISIBLE);			

			double fees = rate.getFeesPerTimeIntervall(lenghView.getTimeIntervall());
			lblFees.setText("Fees  : " + String.valueOf(fees));			
		} else {
			lblFees.setText("no rate for this time");			
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		Log.i("NumberInfoActivity", "Destroying the activity ");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();

		Log.i("NumberInfoActivity", "Pausing the activity ");
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		Log.i("NumberInfoActivity", "Postcreating the activity ");
	}

	@Override
	protected void onPostResume() {
		super.onPostResume();
		Log.i("NumberInfoActivity", "PostResuming the activity");
		bindZones();
	}

	private void bindZones() {
		Log.i("NumberInfoActivity", "binding zones");
		_zones = TelecomCost.getDatabaseManager().getZones();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.i("NumberInfoActivity", "Restarting the activity ");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		Log.i("NumberInfoActivity", "onRestoreInstanceState the activity ");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.i("NumberInfoActivity", "Resuming the activity");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.i("NumberInfoActivity", "onSaveInstanceState the activity");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.i("NumberInfoActivity", "Starting the activity ");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.i("NumberInfoActivity", "Stopping the activity");
	}
}
